package fr.bordeaux1.planingdesetudiants.view;

import java.sql.SQLException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import fr.bordeaux1.planingdesetudiants.model.Cours;
import fr.bordeaux1.planingdesetudiants.model.Enseignant;
import fr.bordeaux1.planingdesetudiants.model.Groupe;
import fr.bordeaux1.planingdesetudiants.model.Ue;

public class AddCours extends Composite {
	private IFenetrePrincipale fenetrePrincipale;
	private Combo listeUE;
	private Combo listeCours;
	private Combo listeProf;
	private Text textSalle;
	private Table listGpe;
	public AddCours(Composite arg0, int arg1,IFenetrePrincipale fenetrePrincipale) {

		super(arg0, arg1);

		this.fenetrePrincipale = fenetrePrincipale;	
		this.setLayout(new FormLayout());

		Label nameE = new Label(this, SWT.NONE);
		nameE.setText("Nom UE :");
		FormData fd_nameE = new FormData();
		fd_nameE.left = new FormAttachment(20, 0);
		fd_nameE.top = new FormAttachment(0, 10);
		nameE.setLayoutData(fd_nameE);
		nameE.setText("Selectionner l'UE :");

		listeUE = new Combo(this, SWT.BORDER | SWT.READ_ONLY);
		fd_nameE.right = new FormAttachment(listeUE, 0,5);
		FormData fd_listeUE = new FormData();
		fd_listeUE.left = new FormAttachment(50, 10);
		fd_listeUE.right = new FormAttachment(100, -10);
		fd_listeUE.top = new FormAttachment(0,5);
		listeUE.setLayoutData(fd_listeUE);
		try {
			java.util.List<Ue> res = fenetrePrincipale.getDb().listerUes();
			for (Ue ue : res){
				listeUE.add(ue.getCode() + " " + ue.getLibelle());
				listeUE.setData(ue.getCode() + " " + ue.getLibelle(), ue);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		listeUE.addSelectionListener(new SelectionListener() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				try {
					listGpe.removeAll();
					Ue ueSelected =(Ue) listeUE.getData(listeUE.getItem(listeUE.getSelectionIndex()));
					java.util.List<Groupe> res = AddCours.this.fenetrePrincipale.getDb().listerGroupeDeUe(ueSelected);
					for (Groupe gpe : res) {
						TableItem item = new TableItem(listGpe, SWT.CHECK
								| SWT.V_SCROLL | SWT.H_SCROLL);
						item.setData(gpe);
						item.setText(gpe.getNom());

					}
				} catch (SQLException e) {
					e.printStackTrace();
				}

			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {				
			}
		});


		Label type = new Label(this, SWT.NONE);
		FormData fd_lblType = new FormData();
		fd_lblType.top = new FormAttachment(15, 21);
		fd_lblType.left = new FormAttachment(20, 0);
		fd_lblType.right = new FormAttachment(50, 0);
		type.setLayoutData(fd_lblType);
		type.setText("Type");

		listeCours = new Combo(this, SWT.BORDER);
		FormData fd_listeCours = new FormData();
		fd_listeCours.left = new FormAttachment(type,10);
		fd_listeCours.top = new FormAttachment(15,20);
		fd_listeCours.right = new FormAttachment(100,-10);
		listeCours.setLayoutData(fd_listeCours);
		listeCours.add(Cours.TYPE_COURS_MAGISTRAL);
		listeCours.add(Cours.TYPE_TD);
		listeCours.add(Cours.TYPE_TP);

		Label prof = new Label(this, SWT.NONE);
		FormData fd_prof = new FormData();
		fd_prof.top = new FormAttachment(listeCours,10);
		fd_prof.left = new FormAttachment(20, 0);
		fd_prof.right = new FormAttachment(50, 0);
		prof.setLayoutData(fd_prof);
		prof.setText("Enseignant :");


		listeProf = new Combo(this, SWT.BORDER | SWT.READ_ONLY);
		FormData fd_listeProf = new FormData();
		fd_listeProf.top = new FormAttachment(listeCours,10);
		fd_listeProf.left = new FormAttachment(prof, 10);
		fd_listeProf.right = new FormAttachment(100,-10);
		listeProf.setLayoutData( fd_listeProf);
		listeProf.add("Non renseigné");
		try {
			java.util.List<Enseignant> res = fenetrePrincipale.getDb().listerEnseignant();
			for (Enseignant enseignant : res) {
				listeProf.add(enseignant.getNom() +" "+ enseignant.getPrenom());
				listeProf.setData(enseignant.getNom() +" "+ enseignant.getPrenom(),enseignant);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		Label salle = new Label(this, SWT.NONE);
		FormData fd_salle = new FormData();
		fd_salle.top = new FormAttachment(listeProf,10);
		fd_salle.left = new FormAttachment(20, 0);
		fd_salle.right = new FormAttachment(50, 0);
		salle.setLayoutData(fd_salle);
		salle.setText("Salle :");
		
		textSalle = new Text(this, SWT.BORDER);
		FormData fd_textSalle = new FormData();
		fd_textSalle.left = new FormAttachment(salle,10);
		fd_textSalle.top = new FormAttachment(listeProf,10);
		fd_textSalle.right = new FormAttachment(100,-10);
		textSalle.setLayoutData(fd_textSalle);

		Label group = new Label(this, SWT.NONE);
		group.setText("Groupes :");
		FormData fd_group = new FormData();
		fd_group.top = new FormAttachment(textSalle,10);
		fd_group.right = new FormAttachment(50,25);
		fd_group.left = new FormAttachment(50,-25);
		group.setLayoutData( fd_group);

		listGpe = new Table(this, SWT.CHECK);
		FormData fd_listGpe = new FormData();
		fd_listGpe.top = new FormAttachment(group,10);
		fd_listGpe.right = new FormAttachment(50,100);
		fd_listGpe.left = new FormAttachment(50,-100);
		fd_listGpe.bottom = new FormAttachment(90,0);
		listGpe.setLayoutData(fd_listGpe);

		Button ok = new Button(this, SWT.NONE);
		FormData fd_ok = new FormData();
		fd_ok.top = new FormAttachment(listGpe,20);
		fd_ok.left = new FormAttachment(50,-75);
		fd_ok.right = new FormAttachment(50,75);
		fd_ok.bottom = new FormAttachment(100,-5);
		ok.setLayoutData(fd_ok);
		ok.setText("Créer le cours");
		ok.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				Cours cours = new Cours();
				Ue ueSelected = (Ue) listeUE.getData(listeUE.getItem(listeUE.getSelectionIndex()));	
				if (!(listeProf.getText().equalsIgnoreCase("Non renseigné") || listeProf.getSelectionIndex()== -1)){
					Enseignant ens = new Enseignant();
					ens = (Enseignant) listeProf.getData(listeProf.getText());
					cours.setIdEnseignant(ens.getId());
				}
				cours.setCodeUe(ueSelected.getCode());
				cours.setType(listeCours.getText());
				cours.setSalle(textSalle.getText());
				try {
					AddCours.this.fenetrePrincipale.getDb().ajouterCours(cours);

					TableItem[] gpss = listGpe.getItems();
					for (TableItem tableItem : gpss) {
						if (tableItem.getChecked()) {
							Groupe grp = (Groupe) tableItem.getData();
							if (tableItem.getChecked())
								AddCours.this.fenetrePrincipale.getDb().ajouterGroupeAuCours(grp, cours);
							
						}
					}
				}catch (SQLException e) {
					e.printStackTrace();
				}
				listeUE.deselectAll();
				listeCours.deselectAll();
				listeUE.deselectAll();
				listeProf.deselectAll();
				listGpe.removeAll();
				textSalle.setText("");
				AddCours.this.fenetrePrincipale.modificationApportees();
				
				AddCours.this.fenetrePrincipale.updateStatusBar("Nouveau cours créé avec succès");
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {

			}
		});
	}

}
